package com.example.ceramics.fragment;

import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.ceramics.R;
import com.example.ceramics.activity.DetailActivity;
import com.example.ceramics.activity.HomeActivity;
import com.example.ceramics.adapter.CartAdapter;
import com.example.ceramics.adapter.GoodsAdapter;
import com.example.ceramics.data.Cart;
import com.example.ceramics.data.Order;
import com.example.ceramics.database.dao.CartDaoImpl;
import com.example.ceramics.database.dao.GoodsDaoImpl;
import com.example.ceramics.database.dao.ICartDao;
import com.example.ceramics.database.dao.IGoodsDao;
import com.example.ceramics.database.dao.IOrderDao;
import com.example.ceramics.database.dao.OrderDaoImpl;
import com.example.ceramics.entity.CartEntity;
import com.example.ceramics.entity.GoodsEntity;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class CartFragment extends BaseFragment {

//    private int a = 1;

    private static boolean isNull = true;
    private RecyclerView recyclerView2;
    private GoodsAdapter goodsAdapter;
//    private RefreshLayout refreshLayout;

    //cart_null
    private Button shopping;
    private static ScrollView layoutCartNull;

    //cart
    private RecyclerView recyclerView;
    private static CartAdapter cartAdapter;
    private static LinearLayout layoutCart;
    //    private List<Integer> positionsChoose = new ArrayList<>();
//    private List<GoodsEntity> goodsEntityList = new ArrayList<>();
    private List<GoodsEntity> selectedGoods = new ArrayList<>();
    private double totalPrice = 0;
    private TextView tvPrice;
    private ImageView ivDelete;
    private Button btnBuy;

    private static ICartDao cartDao;
    private IOrderDao orderDao;
    private IGoodsDao goodsDao;

    private static String accountStatic;
    private String account;

    public CartFragment() {
    }

    public static CartFragment newInstance() {
        CartFragment fragment = new CartFragment();
        return fragment;
    }

    @Override
    protected int initLayout() {
        return R.layout.fragment_cart;
    }

    @Override
    protected void initView() {
//        refreshLayout = mRootView.findViewById(R.id.refreshLayout);

        //cart_null
        shopping = mRootView.findViewById(R.id.shopping);
        layoutCartNull = mRootView.findViewById(R.id.layout_cart_null);
        recyclerView2 = mRootView.findViewById(R.id.recyclerView2);

        //cart
        recyclerView = mRootView.findViewById(R.id.recyclerView);
        layoutCart = mRootView.findViewById(R.id.layout_cart);
        tvPrice = mRootView.findViewById(R.id.tv_price);
        ivDelete = mRootView.findViewById(R.id.iv_delete);
        btnBuy = mRootView.findViewById(R.id.btn_buy);
    }

    @Override
    protected void initData() {
        account = getStringFromSp("account");
        accountStatic = account;

        cartDao = new CartDaoImpl(getActivity());
        orderDao = new OrderDaoImpl(getActivity());
        goodsDao = new GoodsDaoImpl(getActivity());

//        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
//            @Override
//            public void onRefresh(RefreshLayout refreshlayout) {
//                refreshlayout.finishRefresh(1000/*,false*/);//传入false表示刷新失败
//                showToast("刷新成功！");
//                switch_layout();
//            }
//        });

        //cart_null
        shopping.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                navigateTo(HomeActivity.class,"tab",1, Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                HomeActivity activity = (HomeActivity) getActivity();
                activity.getViewPager().setCurrentItem(1);
                activity.getCommonTabLayout().setCurrentTab(1);

//                HomeActivity activity = (HomeActivity) getActivity();
//                activity.reLoadFragView();

                //测试切换布局
//                switch_layout();
            }
        });

        GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 2);
        recyclerView2.setLayoutManager(gridLayoutManager);
        goodsAdapter = new GoodsAdapter(getActivity());
        recyclerView2.setAdapter(goodsAdapter);
        goodsAdapter.setOnItemClickListener(new GoodsAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(Serializable obj) {
                GoodsEntity goodsEntity = (GoodsEntity) obj;
                navigateTo(DetailActivity.class, "detail", goodsEntity);
            }
        });
        twoGoodsList();

        //cart
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);

        cartAdapter = new CartAdapter(getContext());
        cartAdapter.setOnItemClickListener(new CartAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(Serializable obj) {
                GoodsEntity goodsEntity = (GoodsEntity) obj;
                navigateTo(DetailActivity.class, "detail", goodsEntity);
            }
        });
        cartAdapter.setOnItemChooseListener(new CartAdapter.OnItemChooseListener() {
            @Override
            public void onItemChoose(double totalPrice) {
//                GoodsEntity goodsEntity = (GoodsEntity) obj;
//                if (isChecked) {
//                    selectedGoods.add(goodsEntity);
//                } else {
//                    selectedGoods.remove(goodsEntity);
//                }
                tvPrice.setText("¥" + totalPrice);//更新总价
            }
        });
        cartAdapter.setOnItemUpdateNumListener(new CartAdapter.OnItemUpdateNumListener() {
            @Override
            public void onItemUpdateNum(double totalPrice) {
                tvPrice.setText("¥" + totalPrice);
            }
        });
        cartAdapter.setOnItemDeleteListener(new CartAdapter.OnItemDeleteListener() {
            @Override
            public void onItemDelete(double totalPrice) {
//                GoodsEntity goodsEntity = (GoodsEntity) obj;
//                if(selectedGoods.contains(goodsEntity)){
//                    selectedGoods.remove(goodsEntity);
//                }
//                cartAdapter.getDatas().remove(goodsEntity);
//                cartAdapter.notifyDataSetChanged();
//                switch_layout();

                tvPrice.setText("¥" + totalPrice);
            }
        });
        recyclerView.setAdapter(cartAdapter);

//        getCartList();
        switch_layout();

        ivDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (cartAdapter.delete()) {
                    tvPrice.setText("¥0.0");
                    showToast("删除成功！");
                    switch_layout();
                }else {
                    showToast("请选择想要删除的商品！");
                }
            }
        });
        btnBuy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Order.addGoodsEntities(cartAdapter.getSelectedGoods());
//                cartAdapter.delete();
//                tvPrice.setText("¥0.00");
//                showToast("结算成功！");
//                switch_layout();

                List<CartEntity> selectGoods = cartAdapter.getSelectedGoods();
                if (!(selectGoods==null||selectGoods.size()==0)) {
                    orderDao.addOrder(selectGoods);
                    cartAdapter.delete();
                    tvPrice.setText("¥0.0");
                    showToast("结算成功！");
                    switch_layout();
                }else {
                    showToast("请选择想要结算的商品！");
                }
            }
        });
    }

    private void twoGoodsList() {
        List<GoodsEntity> list = new ArrayList<>();
        list = goodsDao.listRandom(2);

//        GoodsEntity goodsEntity1 = new GoodsEntity();
//        goodsEntity1.setGoodsName("十二生肖");
//        goodsEntity1.setGoodsPrice(330);
//        goodsEntity1.setGoodsSignature("一些描述：这个是十二生肖");
//        goodsEntity1.setGoodsType("十二生肖系列");
//        goodsEntity1.setGoodsImg(R.mipmap.image3);
//        list.add(goodsEntity1);
//        GoodsEntity goodsEntity2 = new GoodsEntity();
//        goodsEntity2.setGoodsName("陶瓷盲盒");
//        goodsEntity2.setGoodsPrice(56.8);
//        goodsEntity2.setGoodsSignature("一些描述：这个是陶瓷盲盒");
//        goodsEntity2.setGoodsType("十二生肖系列");
//        goodsEntity2.setGoodsImg(R.mipmap.image2);
//        list.add(goodsEntity2);
        goodsAdapter.setDatas(list);
        goodsAdapter.notifyDataSetChanged();
    }

    private void getCartList() {
//        Cart.test();
        List<CartEntity> cartList = cartDao.getCartList(getStringFromSp("account"));
        cartAdapter.setDatas(cartList);
        cartAdapter.notifyDataSetChanged();

//        cartAdapter.setDatas(Cart.getGoodsEntities());
//        cartAdapter.notifyDataSetChanged();
    }


    public static void switch_layout() {
        //测试切换布局
//        if (isNull) {
//            layoutCartNull.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1));
//            layoutCart.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 0));
//            isNull = false;
//        } else {
//            layoutCartNull.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 0));
//            layoutCart.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1));
//            isNull = true;
//        }

        isNull = cartDao.isEmpty(accountStatic);
        if (isNull) {
            layoutCartNull.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1));
            layoutCart.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 0));
        } else {
            layoutCartNull.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 0));
            layoutCart.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1));
            cartAdapter.setDatas(cartDao.getCartList(accountStatic));
            cartAdapter.notifyDataSetChanged();
        }
    }

//    private void getTotalPrice() {
////        goodsEntityList = cartAdapter.getDatas();
////        for (GoodsEntity goodsEntity : goodsEntityList) {
////            if()
////        }
//        totalPrice = 0;
//        for (GoodsEntity goodsEntity : selectedGoods) {
//            totalPrice += goodsEntity.getGoodsPrice();
//        }
//        tvPrice.setText("¥" + totalPrice);
//    }
}